package com.xiaoma.arch

import android.os.Bundle
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.xiaoma.arch.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

    private val dataVm by viewModels<DataVm>()
    private val binding by lazy {
        ActivityMainBinding.inflate(layoutInflater).also {
            it.lifecycleOwner = this
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
        binding.setVariable(BR.dataVm, dataVm)
    }
}

class DataVm : ViewModel() {

    val numberLiveData = MutableLiveData(0)

    fun increase() {
        val number = numberLiveData.value ?: 0
        numberLiveData.value = number + 1
    }

    fun decrease() {
        val number = numberLiveData.value ?: 0
        numberLiveData.value = number - 1
    }

}
